package com.winz.cultureapp.fragment;

import android.os.Build;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.PopupWindow;

import com.mine.xrecyclerview.rcvadapter.OnItemClickListener;
import com.winz.cultureapp.R;
import com.winz.cultureapp.adapter.xrcv.ExhibitiionArtAdapter;
import com.winz.cultureapp.base.BaseFragment;
import com.winz.cultureapp.entity.ArtListBean;
import com.winz.cultureapp.widge.CustomPopupWindow;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 文化展馆--艺术品
 */
public class ExhibitionArtFragment extends BaseFragment implements OnItemClickListener {
    @BindView(R.id.rcv_art)
    public RecyclerView mRcv_art;

    private CustomPopupWindow mPop_show;

    private List<ArtListBean.DataBean.ListBean> mDatas;
    private ExhibitiionArtAdapter mAdapter;

    private View parentView;//主布局


    public static ExhibitionArtFragment getInstance(){

        ExhibitionArtFragment fragment = new ExhibitionArtFragment();

        return fragment;
    }

    @Override
    protected int contentView() {
        return R.layout.fragment_art;
    }

    @Override
    protected void initViews(View view) {
        mDatas = new ArrayList<>();
        // XrecyclerView
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRcv_art.setLayoutManager(manager);
        mAdapter = new ExhibitiionArtAdapter(mContext,mDatas,R.layout.empty_view,R.layout.item_exhibition_art);
        mPop_show = new CustomPopupWindow(mContext, R.layout.pop_show_img);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            try {
                Field mLayoutInScreen = PopupWindow.class.getDeclaredField("mLayoutInScreen");
                mLayoutInScreen.setAccessible(true);
                mLayoutInScreen.set(mPop_show, true);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }


    }

    @Override
    protected void initEvents() {

        mRcv_art.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(this);

        mPop_show.getWindow().setClickable(false);
        mPop_show.getWindow().findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPop_show.dismiss();
            }
        });
        mPop_show.getWindow().findViewById(R.id.iv_pic).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mPop_show.getWindow().findViewById(R.id.sv_content).getVisibility() == View.VISIBLE){
                    Animation animation = new AlphaAnimation(1,0);
                    animation.setDuration(1000);
                    animation.setRepeatMode(1);
                    mPop_show.getWindow().findViewById(R.id.sv_content).setAnimation(animation);
                    mPop_show.getWindow().findViewById(R.id.sv_content).setVisibility(View.GONE);
                }else{
                    Animation animation = new AlphaAnimation(0,1);
                    animation.setDuration(1000);
                    animation.setRepeatMode(1);
                    mPop_show.getWindow().findViewById(R.id.sv_content).setAnimation(animation);
                    mPop_show.getWindow().findViewById(R.id.sv_content).setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @Override
    protected void loadDatas() {
        getList();
    }

    /**
     * 获取数据列表
     */


    private void getList() {
        mDatas.add(new ArtListBean.DataBean.ListBean());
        mDatas.add(new ArtListBean.DataBean.ListBean());
        mDatas.add(new ArtListBean.DataBean.ListBean());
        mDatas.add(new ArtListBean.DataBean.ListBean());
        mDatas.add(new ArtListBean.DataBean.ListBean());
        mDatas.add(new ArtListBean.DataBean.ListBean());

        mAdapter.setDatas(mDatas);

    }


    @Override
    public void onItemClick(ViewGroup parent, View view, int position) {
        showMsg(position + "");
        mPop_show.showAsDropDown(parentView);
    }

    @Override
    public boolean onItemLongClick(ViewGroup parent, View view, int position) {
        return false;
    }

    public void setParentView(View view){
        parentView = view;
    }
}
